#!/bin/bash

set -e

OPTIND=1
version=

while getopts "v:" opt; do
    case "$opt" in
    v)
        if [[ "$OPTARG" =~ ^[a-z]+$ ]]; then
          version=$OPTARG
        else
          echo "Bad -v value"
          exit 1
        fi
        ;;
    *)
        echo "Wut?"
        exit 1
    esac
done

if test "$version" = ""; then
  echo "Did not provide the distro version [-v]"
  exit 1
fi

dir=$(pwd)
exec_script="$(pwd)/node-ci-exec"

if [ ! -f "$exec_script" ]; then
  echo "Did not provide a node-ci-exec script"
  exit 1
fi

set -x
docker exec node-ci-$version /bin/sh -c "cd $dir && . $exec_script"
